


class TestSoController{
	def testSoInstance = new TestSo()
    	
    def index = { redirect(action:list,params:params) }
    //def index = { redirect(action:toSearch,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ testSoInstanceList: TestSo.list( params ), testSoInstanceTotal: TestSo.count() ]
    }

    def show = {
        def testSoInstance = TestSo.get( params.id )

        if(!testSoInstance) {
            flash.message = "TestSo not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ testSoInstance : testSoInstance ] }
    }

    def delete = {
        def testSoInstance = TestSo.get( params.id )
        if(testSoInstance) {
            try {
                testSoInstance.delete()
                flash.message = "${message(code : 'default.delete.success')}"
                redirect(action:search)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "TestSo ${params.id} could not be deleted"
                redirect(action:edit,id:params.id)
            }
        }
        else {
            flash.message = "TestSo not found with id ${params.id}"
            redirect(action:search)
        }
    }
	
    def checkDuplicated = {name, neId->
        def criteria = TestSo.createCriteria()
        def result = criteria{
            and {
                if(name) like('name', '%' + name + '%')
				//For update, need exclude itself
				if(neId) ne('id', neId.toLong())
				
				//eq('name', name)
				//eq('organization.id',  orgId.toLong())
            }
        }
        return result.size() > 0
    }	

    def edit = {
        def testSoInstance = TestSo.get( params.id )

        if(!testSoInstance) {
            flash.message = "TestSo not found with id ${params.id}"
            redirect(action:search)
        }
        else {
            return [ testSoInstance : testSoInstance ]
        }
    }

    def update = {
        def testSoInstance = TestSo.get( params.id )
        if(testSoInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(testSoInstance.version > version) {
                    
                    testSoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", "Another user has updated this TestSo while you were editing.")
                    render(view:'edit',model:[testSoInstance:testSoInstance])
                    return
                }
            }
			
			//Check if exist same record
			//println 'name='+params.name+", id=" +params.id
			if(checkDuplicated(params.name, params.id)){
				testSoInstance.errors.rejectValue('name', 'default.name.existed')
				render(view:'edit',model:[testSoInstance:testSoInstance])
				return
			}		
            
			testSoInstance.properties = params
            if(!testSoInstance.hasErrors() && testSoInstance.save()) {
                flash.message = "${message(code : 'default.update.success')}"
				render(view:'edit',model:[testSoInstance:testSoInstance])
            }
            else {
                render(view:'edit',model:[testSoInstance:testSoInstance])
            }
        }
        else {
            flash.message = "TestSo not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def testSoInstance = new TestSo()
        testSoInstance.properties = params
        return ['testSoInstance':testSoInstance]
    }

    def save = {
        def testSoInstance = new TestSo(params)
		
        //Check if exist same name
		if(checkDuplicated(params.name, "")){
            testSoInstance.errors.rejectValue('name', 'default.name.existed')
            render(view:'create',model:[testSoInstance:testSoInstance])
        }
		
        if(!testSoInstance.hasErrors() && testSoInstance.save()) {
            flash.message = "${message(code : 'default.create.success')}"
            render(view:'edit',model:[testSoInstance:testSoInstance])
        }
        else {
            render(view:'create',model:[testSoInstance:testSoInstance])
        }
    }
	
	def toSearch = {
        render(view:"search")
    }
	
	def search = {
		int count = 0;
        testSoInstance = testSoInstance?testSoInstance : new TestSo()
        testSoInstance.properties = params

        //println testSoInstance.name
        params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)
        params.offset = params.offset ? params.offset:0
        
		def criteria = TestSo.createCriteria()
		def result = criteria{
			and {
				if(params.name) like('name', '%' + params.name + '%')
				//if(params.name) eq('name', params.name)

				//Paging
                firstResult(params.offset.toInteger())
                maxResults(params.max)				
				
				//Sorting
                if(!params.sort){
                    params.sort = "name"
                }
                if(!params.order){
                    params.order = "desc"
                }
                order(params.sort, params.order)				
			}
		}
		
		if(result.size()==0){
			flash.message = "${message(code : 'default.norecord.found')}"
		}
		else{
			flash.message = ""
		
			//Result Count
			def criteria4Count = TestSo.createCriteria()
			count = criteria4Count.get{
				and {
					if(params.name) like('name', '%' + params.name + '%')
				}
				projections { 
					rowCount() 
				}                  
			}
		}            
		[ 'testSoInstanceList': result, 'testSoInstanceTotal': count, 'testSoInstance' : testSoInstance]
    }
}

class TestSoSO {
	
	
	
}